package com.asyf.demo.controller;

import com.asyf.demo.annotation.MyDS;
import com.asyf.demo.common.Result;
import com.asyf.demo.entity.User;
import com.asyf.demo.entity.User2;
import com.asyf.demo.mapper.User2Mapper;
import com.asyf.demo.mapper.UserMapper;
import com.asyf.demo.service.MyBatisPlusService;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yjz
 * @Date 2021/8/18 10:17
 */
@RestController
@RequestMapping(value = "/mybatisplus")
@Api(tags = "mybatisplus测试")
public class MybatisPlusController {

    @Resource
    private UserMapper userMapper;

    @Resource
    private User2Mapper user2Mapper;

    @Autowired
    private MyBatisPlusService myBatisPlusService;

    //    @DS(value = "slave")
    @RequestMapping(value = "test", method = RequestMethod.POST)
    @ApiOperation(value = "查询用户列表")
    public Object test() {
        List<User> userList = userMapper.selectList(null);
        return Result.success(userList);
    }

    @MyDS
    @RequestMapping(value = "findUserList", method = RequestMethod.POST)
    @ApiOperation(value = "查询用户列表-分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", required = true, type = "int", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, type = "int", example = "3")
    })
    public Object findUserList(Integer pageNum, Integer pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Page<User> page = new Page<>(pageNum, pageSize);
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);
        return Result.success(userPage);
    }

    @RequestMapping(value = "findUserById", method = RequestMethod.POST)
    @ApiOperation(value = "查询用户-自定义xml")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, type = "string", example = "1")
    })
    public Object findUserById(String id) {
        User user = userMapper.findUserById(id);
        return Result.success(user);
    }

    //@DS(value = "slave")
    @RequestMapping(value = "test2", method = RequestMethod.POST)
    @ApiOperation(value = "动态切换数据源-手动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataSource", value = "用户id", required = true, type = "string", defaultValue = "slave2")
    })
    public Object test2(String dataSource) {
        // 切换数据源 slave 为配置的名称
        DynamicDataSourceContextHolder.push(dataSource);
        List<User> userList = userMapper.selectList(null);
        // 清理
        DynamicDataSourceContextHolder.clear();
        return Result.success(userList);
    }

    @RequestMapping(value = "test3", method = RequestMethod.POST)
    @ApiOperation(value = "动态切换数据源-header")
    @ApiImplicitParams({
    })
    public Object test3() {
        List<User> userList = userMapper.selectList(null);
        return Result.success(userList);
    }

    @RequestMapping(value = "test4", method = RequestMethod.POST)
    @ApiOperation(value = "事务测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num", value = "num", required = true, type = "int", defaultValue = "0")
    })
    public Object test4(Integer num) {
        myBatisPlusService.saveTest3(num);
//        myBatisPlusService.saveTest(num);//数据源切换是无效的，@DS本类代理： 本类方法不经过代理，无法进行切换
        return Result.success("ok");
    }


    @RequestMapping(value = "test5", method = RequestMethod.POST)
    @ApiOperation(value = "自定义注解切换数据源")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num", value = "num", required = true, type = "int", defaultValue = "0")
    })
    public Object test5(Integer num) {
        List<User> userList = myBatisPlusService.test5(num);
        return Result.success(userList);
    }

    @RequestMapping(value = "test6", method = RequestMethod.POST)
    @ApiOperation(value = "insert测试-字段不同")//不生效 password并没有插入
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num", value = "num", required = true, type = "int", defaultValue = "0")
    })
    public Object test6(Integer num) {
        myBatisPlusService.test6(num);
        return Result.success("ok");
    }

    @RequestMapping(value = "test7", method = RequestMethod.POST)
    @ApiOperation(value = "insert测试-字段不同(xml)")//不生效 password并没有插入
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num", value = "num", required = true, type = "int", defaultValue = "0")
    })
    public Object test7(Integer num) {
        myBatisPlusService.test7(num);
        return Result.success("ok");
    }

    @MyDS
    @RequestMapping(value = "test8", method = RequestMethod.POST)
    @ApiOperation(value = "多字段查询少字段")//不生效 password并没有插入
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, type = "string", defaultValue = "1")
    })
    public Object test8(String id) {
//        User2 user = user2Mapper.findUserById(id);//select * 不报错
        Map<String, Object> map = new HashMap<>();
        List<User2> users = user2Mapper.selectByMap(map);//会报错
        return Result.success(users);
    }


    @RequestMapping(value = "test9", method = RequestMethod.POST)
    @ApiOperation(value = "连接泄露测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "num", value = "num", required = true, type = "int", defaultValue = "0")
    })
    public Object test9(Integer num) {
        myBatisPlusService.test9(num);
//        myBatisPlusService.saveTest(num);//数据源切换是无效的，@DS本类代理： 本类方法不经过代理，无法进行切换
        return Result.success("ok");
    }

}
